#include <string>
#include <vector>
#include <iostream>

#include <SFML/Graphics.hpp>

#include "app.h"
#include "image_manager.h"
#include "sprite_manager.h"

// Main Function
int main(int argc, char *argv[])
{
	std::cout << "(c) 2009, Keunhong Park, All Rights Reserved." << std::endl;
	
	// Initialize App class
	App::init();
	
	// Use Vertical Sync
	//  - Ultimately caps the framerate decreasing the amount of CPU cycles consumed
	//     - Ergo, it becomes less of a hog.
	App::window.UseVerticalSync(true); 
	
	// Load awesome image
	App::image_manager.set_image("data/awesome.png");
	App::sprite_manager.set_sprite(App::image_manager.get_image("data/awesome.png"), 200, 200, "awesome");

	std::vector<sf::Sprite*> sprites;
	
	sf::Sprite* awesome_sprite = App::sprite_manager.get_sprite("awesome");
	// Main Loop
	while(App::window.IsOpened() == true)
	{
		
		//std::cout << 1.f / App::window.GetFrameTime() << std::endl;
		
		App::elapsed_time = App::window.GetFrameTime();
		
		while(App::window.GetEvent(App::event))
		{
			// If window is closed, terminate program
			if(App::event.Type == sf::Event::Closed)
			{
				App::window.Close();
			}

			// If escape key is pressed, terminate program
			if(App::event.Type == sf::Event::KeyPressed && App::event.Key.Code == sf::Key::Escape)
			{
				App::window.Close();
			}
		}

		// Clear screen
		App::window.Clear(sf::Color(0,0,0));
		
		
		
		
		
		
		// Move the sprite
		if (App::window.GetInput().IsKeyDown(sf::Key::A))  awesome_sprite->Move(-500 * App::elapsed_time, 0);
		if (App::window.GetInput().IsKeyDown(sf::Key::D)) awesome_sprite->Move( 500 * App::elapsed_time, 0);
		if (App::window.GetInput().IsKeyDown(sf::Key::W))    awesome_sprite->Move(0, -500 * App::elapsed_time);
		if (App::window.GetInput().IsKeyDown(sf::Key::S))  awesome_sprite->Move(0,  500 * App::elapsed_time);
		
		// Rotate the sprite
		if (App::window.GetInput().IsKeyDown(sf::Key::Right))      awesome_sprite->Rotate(-100 * App::elapsed_time);
		if (App::window.GetInput().IsKeyDown(sf::Key::Left)) awesome_sprite->Rotate( 100 * App::elapsed_time);		
		
		
		
		
		
		
		//if(awesome_sprite->GetPosition().x == 0){
			//awesome_sprite
		//}
		
		
		
		
		
		
		
		
		// Loop through all sprites and draw them
		// Get list of sprites and refresh the sprites variable
		// Use vector iterator to loop through sprites.
		sprites = App::sprite_manager.get_all_sprites();
		for(std::vector<sf::Sprite*>::iterator iterator = sprites.begin();
			iterator != sprites.end(); ++iterator)
		{
			// Draw each sprite.
			//  Dereference the iterator once to get to the actual item and once again
			//  since the item is a pointer to the sprite.
			App::window.Draw(**iterator);
		}
		
		// Display/Refresh screen
		App::window.Display();
	}

	// Return EXIT_SUCCESS and terminate program
	//   EXIT_SUCCESS is a constant defined as 0
	return EXIT_SUCCESS;

}
